﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ImaginationFactory.Dialogs
{
    /// <summary>
    /// UsefulCutOut.xaml 的交互逻辑
    /// </summary>
    public partial class UsefulCutOut : Window
    {
        MainWindow m_Window;
        //MainListBox m_ListBox;

        public UsefulCutOut( MainWindow mWindow )
        {
            InitializeComponent();

            m_Window = mWindow;     

            image.Source = m_Window.ImageLibrary.LoadedImage.Source;
        }




        private void image_ImageFailed( object sender, ExceptionRoutedEventArgs e )
        {
            this.Close();
        }


        private void Window_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
            this.DragMove();
        }


        bool bLeftButtonDown = false;
        Point oldPoint = new Point( 0, 0 );
        Point pt = new Point( 0, 0 );
        Point MouseOldPt = new Point( 0, 0 );
        Point MousePt = new Point( 0, 0 );

        private void image_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
            MyCover.Children.Clear();

            oldPoint = e.GetPosition( MyCover );
            MouseOldPt = e.GetPosition( image );
            Mouse.Capture( image );

            bLeftButtonDown = true;            
        }

        private void image_MouseLeftButtonUp( object sender, MouseButtonEventArgs e )
        {
            bLeftButtonDown = false;
            pt = e.GetPosition( MyCover );
            MousePt = e.GetPosition( image );
            image.ReleaseMouseCapture();
        }

        private void image_MouseMove( object sender, MouseEventArgs e )
        {
            if ( bLeftButtonDown && image.CaptureMouse() )
            {
                pt = e.GetPosition( MyCover );

                RectangleGeometry rect = new RectangleGeometry( new Rect( oldPoint, pt ) );

                Path myPath = new Path();
                Color color = new Color();
                color = Colors.Gray;
                color.A = 100;
                myPath.Fill = new SolidColorBrush( color );
                myPath.Stroke = Brushes.Black;
                myPath.StrokeThickness = 1;
                myPath.Data = rect;   

                MyCover.Children.Clear();
                MyCover.Children.Add( myPath );
            }
        }

        private void SubGrid_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
            MessageBox.Show( "SubGrid_MouseLeftButtonDown" );
        }

        private void MyCover_MouseLeftButtonDown( object sender, MouseButtonEventArgs e )
        {
            MessageBox.Show( "MyCover_MouseLeftButtonDown" );
        }

        //
        // 确定
        //
        private void Ok_Click( object sender, RoutedEventArgs e )
        {
            if ( !( MouseOldPt.X == 0 && MouseOldPt.Y == 0 || MousePt.X == 0 && MousePt.Y == 0 || MouseOldPt == MousePt ) )
            {
                // 修正坐标
                double xRate = m_Window.ImageLibrary.Width / image.ActualWidth;
                double yRate = m_Window.ImageLibrary.Height / image.ActualHeight;                

                MouseOldPt.X *= xRate;
                MouseOldPt.Y *= yRate;
                MousePt.X *= xRate;
                MousePt.Y *= yRate;                

                m_Window.ImageLibrary.CutOutImage( MouseOldPt, MousePt );                
                m_Window.image.Source = m_Window.ImageLibrary.LoadedImage.Source;
                m_Window.ResetTransform();
            }

            this.Close();
        }

        private void Cancel_Click( object sender, RoutedEventArgs e )
        {
            this.Close();
        }





    }
}
